<?php

require_once 'const/DBConst.php';
require_once 'business/AccountManager.php';

class Application_Model_AccountMapper {

    public static function insert($account) {
        $db = Zend_Registry::get("database");
        $date = array(
            C_EMAIL => $account->getEmail(),
            C_PASSWORD => $account->getPassword(),
            C_TYPE => $account->getType()
        );
        $db->insert(T_ACCOUNT, $date);
        return $db->lastInsertId();
    }

    public static function getIDByEmail($email) {
        $db = Zend_Registry::get("database");
        $result = $db->fetchRow("SELECT ID FROM ACCOUNT WHERE EMAIL='" . $email . "'");
        return $result["ID"];
    }

    public static function update($account, $oldPassword) {
        $success = Business_AccountManager::verify($account->getEmail(), $oldPassword);
        if ($success) {
            $db = Zend_Registry::get("database");
            $date = array(
                C_EMAIL => $account->getEmail(),
                C_PASSWORD => $account->getPassword(),
            );
            $db->update(T_ACCOUNT, $date, "EMAIL='" . $account->getEmail() . "'");
        }
        return $success;
    }

}

